<?php 
defined('RUN_KATANA') or die('Restricted access');
class UserLocation extends Object
{
	public $UserId;
	public $Country;
	public $CountryName;
	public $State;
	public $City;
	public $Zip;
	public $Address;
	
	public function getUserId() { return intval($this->UserId); }
	public function setUserId($val) { $this->UserId=intval($val); }
	
	public function UserLocation($values = Array())
	{ 
		self::setDataTable('user_locations');
		if(count($values) > 0)
		{ self::setValues($values); }
	}
	
	public function __toString()
	{
		if(self::getCountryName() != '')
		{ $out .= self::getCountryName().', '; }
		if(self::getCity() != '')
		{ $out .= self::getCity().', '; }
		if(self::getAddress() != '')
		{ $out .= self::getAddress().' '; }
		$out .= self::getZip();
		return $out;
	}
	
	public function insert()
	{
		if(self::getUserId() > 0)
		{
			$sql  = 'INSERT INTO '.DB_PERFIX.self::getDataTable();
			$sql .= '(`userId`, `country`, `state`, `city`, `zip`, `address`) '; 
			$sql .= 'VALUES ';
			$sql .= '('.self::getUserId().', ';
			$sql .= '"'.self::formatText(self::getCountry()).'", ';
			$sql .= '"'.self::formatText(self::getState()).'", ';
			$sql .= '"'.self::formatText(self::getCity()).'", ';
			$sql .= '"'.self::formatText(self::getZip()).'", ';
			$sql .= '"'.self::formatText(self::getAddress()).'" ';
			$sql .= ');';
			$result = mysql_query($sql);
		}
	}
	
	public function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			global $user;
			if(($user->getId() == self::getUserId()) || ($user->getLevel() > 4))
			{
				$sql  = 'UPDATE '.DB_PERFIX.self::getDataTable().' SET ';
				$sql .= '`userId`='.self::getUserId().', ';
				$sql .= '`country`="'.self::formatText(self::getCountry()).'", ';
				$sql .= '`state`="'.self::formatText(self::getState()).'", ';
				$sql .= '`city`="'.self::formatText(self::getCity()).'", ';
				$sql .= '`zip`="'.self::formatText(self::getZip()).'", ';
				$sql .= '`address`="'.self::formatText(self::getAddress()).'" ';
				$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
				mysql_query($sql);
			}
		}
	}
}
?>